<?php

namespace App\Jobs;

use App\Config;
use App\Coupon;
use App\CouponUse;
use App\Goods;
use App\MchSponsorTem;
use App\Order;
use App\OrderAcm;
use App\OrderDetail;
use App\SponsorTem;
use App\User;
use App\Wallet;
use App\WalletConfig;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class handleOrderPaid implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $order_number;

    /**
     * Create a new job instance.
     *
     * @param $order_number
     */
    public function __construct($order_number)
    {
        $this->order_number=$order_number;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $order=Order::where('order_number',$this->order_number)->first();
        if ($order->status==0){
            $order->status=0;
            $order->pay_type=1;
            $order->pay_time=Carbon::now();
            $order->save();
            $data=Redis::hmget("mch_order_".$order->mch_id,['new_orders','paid_orders','paid_amount']);
            Redis::hmset("mch_order_".$order->mch_id,['paid_orders'=>$data[1]+1,'paid_amount'=>$data[2]+$order->pay_amount]);
        }
        $order_acm=OrderAcm::where([['mch_id','=',$order->mch_id],['date','=',date('Y-m-d')]])->first();
        if ($order_acm){
            $order_acm->awaiting_order_number-=1;
            $order_acm->paid_order_number+=1;
            $order_acm->total_amount+=$order->pay_amount;
            $order_acm->save();
        }
        if (floatval($order->deduction_wallet_money)>0){
            $wallet_order=Wallet::where('order_number',$order->order_number)->first();
            $wallet_order->status=1;
            $wallet_order->save();
        }
        $detail=OrderDetail::where('order_id',$this->order_number)->get();
        $has_vip=0;
        $vip_price=0;
        foreach ($detail as $key=>$value){
            $goods=Goods::where('id',$value->goods_id)->first();
            $tags=json_decode($goods['tags']);
            if (isset($tags['vip'])&&$tags['vip']==1){
               $has_vip=1;
               $vip_price=$value['price'];
            }
            Goods::where('pid',$goods->pid)->update(['stock'=>DB::raw('stock-'.$value['number'])]);
        }
        $where=[
            ['mch_id','=',]
        ];
//        $config=Config::where()
        if ($has_vip==1){
            User::where('id',$order->uid)->update(['current_status'=>1]);
            $coupon_list=Coupon::where('mch_id',$order->mch_id)->where('status',1)->get();
            foreach ($coupon_list as $key=>$value){
                for ($i=0;$i<$value['couponNumber'];$i++){
                    $data=[
                        'uid'=>$order->uid,
                        'mch_id'=>$order->mch_id,
                        'useStatus'=>0,
                        'period_time'=>Carbon::now()->addDays($value['period']),
                        'range'=>$value['range'],
                        'value'=>$value['couponValue'],
                        'coupon_id'=>$value['id'],
                        'sys_give'=>1
                    ];
                    CouponUse::create($data);
                }

            }
            //发放奖金
            $where=[
                ['mch_id','=',$order->mch_id]
            ];
            $wallet_config=WalletConfig::where($where)->first();
            //如果开启直推入奖金则进行如下操作
            if ($wallet_config&&$wallet_config['openBuy']==1){
                $sponsor_tem=MchSponsorTem::where('mch_id',$order->mch_id)->first();
                if ($sponsor_tem){
                    $amount=0;
                    if ($sponsor_tem['rate_check']==0){
                        $amount=$vip_price*floatval($sponsor_tem['value'])/100;
                    }else{
                         $amount=$sponsor_tem['value'];
                    }
                    $this->check($order->uid,$sponsor_tem,$amount,$amount);
                }
            }else{ //否则进行如下操作
                $sponsor_tem=MchSponsorTem::where('mch_id',$order->mch_id)->first();
                if ($sponsor_tem){
                    $amount=0;
                    if ($sponsor_tem['rate_check']==0){
                        $amount=$vip_price*floatval($sponsor_tem['value'])/100;
                    }else{
                        $amount=$sponsor_tem['value'];
                    }
                    $this->no_check($order->uid,$sponsor_tem,$amount,$amount);
                }
            }
        }

    }
    public function check($uid,$template,$amount,$order_number)
    {
        $user=User::find($uid);
        $sponsor_id=$user['sponsor_id'];
        if ($sponsor_id){
            $count_sponsor=User::where('sponsor_id',$sponsor_id)->count();
            if ($count_sponsor>=$template['sponsor_num']){
                if ($template['template_id']==0){
                    if ($template['template_config']['sponsor_day_live']==1){
                        $sponsor=User::where('id',$sponsor_id)->first();
                        $amount=$template['template_config']['money_superposition']?$amount*$template['sponsor_num']:$amount;
                        $time=Carbon::now();
                            for($i=0;$i<30;$i++){
                                $data=[
                                    'uid'=>$sponsor_id,
                                    'amount'=>$amount/30,
                                    'source'=>1,
                                    'status'=>(strtotime($sponsor['expire_time'])>=time()&&$sponsor['current_status']==1)&&$i==0?1:2,
                                    'order_number'=>$order_number,
                                    'mch_id'=>$template['mch_id'],
                                    'type'=>1,
                                    'effective_time'=>$time
                                ];
                                Wallet::create($data);
                                $time=$time->addDays(1);
                            }

                    }else{
                        $time=Carbon::now();
                        for($i=0;$i<30;$i++){
                            $data=[
                                'uid'=>$sponsor_id,
                                'amount'=>$amount/30,
                                'source'=>1,
                                'status'=>1,
                                'order_number'=>$order_number,
                                'mch_id'=>$template['mch_id'],
                                'type'=>1,
                                'effective_time'=>$time
                            ];
                            Wallet::create($data);
                            $time=$time->addDays(1);
                        }
                    }
                }elseif ($template['template_id']==1){
                    $data=[
                        'uid'=>$sponsor_id,
                        'amount'=>$amount,
                        'source'=>1,
                        'status'=>2,
                        'order_number'=>$order_number,
                        'mch_id'=>$template['mch_id'],
                        'type'=>1,
                        'effective_time'=>Carbon::now()->firstOfMonth()->addMonth()
                    ];
                    Wallet::create($data);
                }elseif ($template['template_id']==3){
                    $sponsor=User::where('id',$sponsor_id)->first();
                    $amount=$template['template_config']['money_superposition']?$amount*$template['sponsor_num']:$amount;
                    if ($template['template_config']['sponsor_buy_goods']==1){
                        if ($sponsor['current_status']==1){
                            $data=[
                                'uid'=>$sponsor_id,
                                'amount'=>$amount,
                                'source'=>1,
                                'status'=>1,
                                'order_number'=>$order_number,
                                'mch_id'=>$template['mch_id'],
                                'type'=>1,
                                'effective_time'=>Carbon::now()
                            ];
                            Wallet::create($data);
                        }
                    }else{
                        $data=[
                            'uid'=>$sponsor_id,
                            'amount'=>$amount,
                            'source'=>1,
                            'status'=>1,
                            'order_number'=>$order_number,
                            'mch_id'=>$template['mch_id'],
                            'type'=>1,
                            'effective_time'=>Carbon::now()
                        ];
                        Wallet::create($data);
                    }
                }
            }
        }
    }
    public function no_check($uid,$template,$amount,$order_number)
    {
        $user=User::find($uid);
        $sponsor_id=$user['sponsor_id'];
        if ($sponsor_id){
            $count_sponsor=User::where('sponsor_id',$sponsor_id)->count();
            if ($count_sponsor>=$template['sponsor_num']){
                if ($template['template_id']==0){
                    if ($template['template_config']['sponsor_day_live']==1){
                        $sponsor=User::where('id',$sponsor_id)->first();
                        $amount=$template['template_config']['money_superposition']?$amount*$template['sponsor_num']:$amount;
                        $time=Carbon::now();
                        for($i=0;$i<30;$i++){
                            $data=[
                                'uid'=>$sponsor_id,
                                'amount'=>$amount/30,
                                'source'=>1,
                                'status'=>2,
                                'order_number'=>$order_number,
                                'mch_id'=>$template['mch_id'],
                                'type'=>2,
                                'effective_time'=>$time
                            ];
                            Wallet::create($data);
                            $time=$time->addDays(1);
                        }

                    }else{
                        $time=Carbon::now();
                        for($i=0;$i<30;$i++){
                            $data=[
                                'uid'=>$sponsor_id,
                                'amount'=>$amount/30,
                                'source'=>1,
                                'status'=>2,
                                'order_number'=>$order_number,
                                'mch_id'=>$template['mch_id'],
                                'type'=>2,
                                'effective_time'=>$time
                            ];
                            Wallet::create($data);
                            $time=$time->addDays(1);
                        }
                    }
                }elseif ($template['template_id']==1){
                    $data=[
                        'uid'=>$sponsor_id,
                        'amount'=>$amount,
                        'source'=>1,
                        'status'=>2,
                        'order_number'=>$order_number,
                        'mch_id'=>$template['mch_id'],
                        'type'=>2,
                        'effective_time'=>Carbon::now()->firstOfMonth()->addMonth()
                    ];
                    Wallet::create($data);
                }elseif ($template['template_id']==3){
                    $sponsor=User::where('id',$sponsor_id)->first();
                    $amount=$template['template_config']['money_superposition']?$amount*$template['sponsor_num']:$amount;
                    if ($template['template_config']['sponsor_buy_goods']==1){
                        if ($sponsor['current_status']==1){
                            $data=[
                                'uid'=>$sponsor_id,
                                'amount'=>$amount,
                                'source'=>1,
                                'status'=>2,
                                'order_number'=>$order_number,
                                'mch_id'=>$template['mch_id'],
                                'type'=>2,
                                'effective_time'=>Carbon::now()
                            ];
                            Wallet::create($data);
                        }
                    }else{
                        $data=[
                            'uid'=>$sponsor_id,
                            'amount'=>$amount,
                            'source'=>1,
                            'status'=>2,
                            'order_number'=>$order_number,
                            'mch_id'=>$template['mch_id'],
                            'type'=>2,
                            'effective_time'=>Carbon::now()
                        ];
                        Wallet::create($data);
                    }
                }
            }
        }
    }
}
